﻿using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.StyleElements
{
    [XmlRoot("colors", Namespace = Constants.ExcelMain)]
    public class Colors : XmlSerializable
    {
        [Optional]
        [XmlElement("indexedColors", Namespace = Constants.ExcelMain)]
        public ColorIndexes ColorIndexes { get; set; }

        [Optional]
        [XmlElement("mruColors", Namespace = Constants.ExcelMain)]
        public MRUColors MRUColors { get; set; }

        public Colors() { }
    }

    [XmlRoot("indexedColors", Namespace = Constants.ExcelMain)]
    public class ColorIndexes : XmlSerializable
    {
        [Optional]
        [XmlElement("rgbColor", Namespace = Constants.ExcelMain)]
        public XmlList<RGBColor> RGBColors { get; set; }

        public ColorIndexes() 
        {
            RGBColors = new XmlList<RGBColor>();
        }
    }

    [XmlRoot("rgbColor", Namespace = Constants.ExcelMain)]
    public class RGBColor : XmlSerializable
    {
        /// <summary>
        /// Hexadecimal number. Length: 4
        /// </summary>
        [Optional]
        [XmlAttribute("rgb", Form = XmlSchemaForm.Qualified)]
        public string RGB
        {
            get { return color; }
            set
            {
                if (value == null || (value.Length == 4 && !Helpers.ParseHexadecimalValue(value)))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 4!");
                }

                color = value;
            }
        }

        [XmlIgnore]
        private string color;

        public RGBColor() { }
    }

    [XmlRoot("mruColors", Namespace = Constants.ExcelMain)]
    public class MRUColors : XmlSerializable
    {
        [XmlElement("color", Namespace = Constants.ExcelMain)]
        public XmlList<Color> Colors { get; set; }

        public MRUColors()
        {
            Colors = new XmlList<Color>();
        }
    }

    [XmlRoot("color", Namespace = Constants.ExcelMain)]
    public class Color : XmlSerializable
    {
        [Optional]
        [XmlAttribute("auto", Form = XmlSchemaForm.Qualified)]
        public Nullable<bool> Automatic { get; set; }

        [Optional]
        [XmlAttribute("indexed", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> Index { get; set; }

        /// <summary>
        /// Hexadecimal number. Length: 8
        /// </summary>
        [Optional]
        [XmlAttribute("rgb", Form = XmlSchemaForm.Qualified)]
        public string RBG
        {
            get { return color; }
            set
            {
                if (value == null || (value.Length == 8 && !Helpers.ParseHexadecimalValue(value)))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 8!");
                }

                color = value;
            }
        }

        [Optional]
        [XmlAttribute("theme", Form = XmlSchemaForm.Qualified)]
        public Nullable<uint> Theme { get; set; }

        [Optional]
        [XmlAttribute("tint", Form = XmlSchemaForm.Qualified)]
        public Nullable<double> Tint { get; set; }

        [XmlIgnore]
        private string color;

        public Color() { }
    }
}
